//
//  StudyRightView.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/3/29.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "StudyRightView.h"
#import "signUpCollectionCell.h"
#import "MyWorkVC.h"

@interface StudyRightView ()
<UICollectionViewDataSource,UICollectionViewDelegate,ResquestServicesDelegate>

@end

@implementation StudyRightView
{
    RequestServices *service;
    NSInteger page_index;//页码
    NSMutableArray *list_arr;//列表数组
}
-(void)addView{
    service = [[RequestServices alloc]init];
    service.delegate = self;
    [self setupUI];
    list_arr = [[NSMutableArray alloc]init];
    [self headerRefresh];
    //左划
    self.mj_trailer =[MJRefreshTrailer trailerWithRefreshingBlock:^{
        [self footerRefresh];
    }];
    self.mj_trailer.hidden = YES;
}

-(void)setupUI{
    self.delegate = self;
    self.dataSource = self;
    self.showsHorizontalScrollIndicator = NO;
    self.backgroundColor = [UIColor clearColor];
    [self registerNib:[UINib nibWithNibName:@"signUpCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"signUpCollectionCell"];
}

- (void)headerRefresh
{ //  self.service.view = self;
    page_index = 1;
    [service tuiJianZuoPin:[NSString stringWithFormat:@"%ld",page_index] tag:8000];
}

- (void)footerRefresh
{
    page_index++;
    [service tuiJianZuoPin:[NSString stringWithFormat:@"%ld",page_index] tag:8001];
}


#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self.VC HideLoading];
//    [self.mj_trailer endRefreshing];

    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
    if (tag == 8000) {
        if ([tempArr CheckNSArrayData]) {
            list_arr = [AliefuModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        }
    }else {
        [list_arr addObjectsFromArray:[AliefuModel mj_objectArrayWithKeyValuesArray:tempArr]];
    }
    
    [self reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self.mj_trailer endRefreshing];
    [self.VC ShowNewMsg:error.domain];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return list_arr.count;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((collectionView.frame.size.width - 60)/3, (collectionView.frame.size.height)*0.75);
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    signUpCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signUpCollectionCell" forIndexPath:indexPath];
    cell.model = list_arr[indexPath.item];
    return cell;
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView
    layout:(UICollectionViewLayout *)collectionViewLayout
    insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0.0f, 20.0f, 0.0f, 0.0f);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MyWorkVC *vc = [[MyWorkVC alloc]init];
    AliefuModel *model = list_arr[indexPath.row];
    vc.valueDic = [model mj_keyValues];
    [self.VC PushToViewController:vc];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
